package com.example.a_java.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.a_java.common.Result;
import com.example.a_java.entity.SysMenu;
import com.example.a_java.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "菜单管理", description = "菜单管理相关接口")
@RestController
@RequestMapping("/system/menu")
@SecurityRequirement(name = "bearer-key")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    @Operation(summary = "获取菜单树形列表")
    @GetMapping("/treeList")
    @PreAuthorize("hasAuthority('system:menu:list')")
    public Result<List<SysMenu>> treeList(
            @Parameter(description = "菜单名称") @RequestParam(required = false) String menuName,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<SysMenu>()
                .like(StringUtils.hasText(menuName), SysMenu::getMenuName, menuName)
                .eq(status != null, SysMenu::getStatus, status)
                .orderByAsc(SysMenu::getSortOrder);
        
        List<SysMenu> menus = menuService.list(wrapper);
        return Result.success(menuService.buildMenuTree(menus));
    }

    @Operation(summary = "获取菜单详情")
    @GetMapping("/{menuId}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public Result<SysMenu> getInfo(@PathVariable Long menuId) {
        return Result.success(menuService.getById(menuId));
    }

    @Operation(summary = "新增菜单")
    @PostMapping
    @PreAuthorize("hasAuthority('system:menu:add')")
    public Result<Boolean> add(@RequestBody @Valid SysMenu menu) {
        return Result.success(menuService.save(menu));
    }

    @Operation(summary = "修改菜单")
    @PutMapping
    @PreAuthorize("hasAuthority('system:menu:edit')")
    public Result<Boolean> update(@RequestBody @Valid SysMenu menu) {
        return Result.success(menuService.updateById(menu));
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{menuId}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    public Result<Boolean> delete(@PathVariable Long menuId) {
        return Result.success(menuService.removeById(menuId));
    }

    @Operation(summary = "获取角色菜单列表")
    @GetMapping("/role/{roleId}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public Result<List<SysMenu>> getRoleMenuList(@PathVariable Long roleId) {
        List<SysMenu> menus = menuService.getMenusByRoleId(roleId);
        return Result.success(menus);
    }
} 
